package com.example.lib_core.http;

import com.fhz.utilsfhz.LiveDataCallAdapterFactory;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {
    private RetrofitManager(){}
    private static volatile RetrofitManager retrofitManager = new RetrofitManager();
    public static synchronized RetrofitManager getInstance(){
        return retrofitManager;
    }

    private Retrofit retrofit;

    public Retrofit getRetrofit(String url){
        if (retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl(url)
                    .addCallAdapterFactory(new LiveDataCallAdapterFactory())
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(new OkHttpClient.Builder()
                            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                            .addInterceptor(new Interceptor() {
                                @Override
                                public Response intercept(Chain chain) throws IOException {
                                    Request request = chain.request().newBuilder().addHeader("Authorization", "bearer ikWZHfW4QH63Xg-I8N8jbXOiocqrEO9JBEPffTC6kYOOsdGwy4p-sPZnodR6jDW1Rp6cJ39kYNp74VFxObrf2aWNgEv7w5-w9qzvpx5AuMIAVsA1y5LDTSghPL_CLHrH6V5JGZdLoRycqnDvTO3jlBgvGIq4YuZ8WNC95atBX6O4LZSBlTIBQz7tZbtUC4fYIjZ0XKkvRteDI2-_a9TDgiZBTZFs5qUDcNBix9TQnyfQenNw0J3d0xjOED824kiGW_2Z3gUMCz3tnLF1LytuSCIi0H_zNwceRnUvPj7bjfE").build();
                                    return chain.proceed(request);
                                }
                            })
                            .readTimeout(30, TimeUnit.MINUTES)
                            .connectTimeout(30,TimeUnit.MINUTES)
                            .writeTimeout(30,TimeUnit.MINUTES)
                            .build())
                    .build();
        }
        return retrofit;
    }
}
